package cn.stazxr.deepseekchat.controller;

import cn.stazxr.deepseekchat.service.DeepSeekService;
import cn.stazxr.deepseekchat.utils.ReturnJsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

/**
 * Controller for DeepSeek
 *
 * @author talentestors
 * @version 1.1
 * @since 2025.3.14
 **/
@Slf4j
@RestController
public class DeepSeekController {

	private DeepSeekService deepSeekService;

	public DeepSeekController() {
	}

	@Autowired
	public DeepSeekController(DeepSeekService deepSeekService) {
		this.deepSeekService = deepSeekService;
	}

	@GetMapping("/models")
	public List<Map<String, Object>> sendModelListRequest() {
		String res = deepSeekService.sendModelListRequest().blockFirst();
		return ReturnJsonUtils.str2Map(res);
	}

	@GetMapping("/chat")
	public Mono<String> sendChatRequest(String question) {
		// http://localhost:8044/chat?question=什么是deepseek？
		Flux<String> response = deepSeekService.sendChatRequest(question);

		// 将 Flux 中的元素收集到一个列表中
		Mono<List<String>> listMono = response.collectList();

		// 将列表中的元素拼接成一个字符串
		return listMono.map(list -> {
			StringBuilder sb = new StringBuilder();
			for (String str : list) {
				sb.append(str);
			}
			System.out.println("resultMono = " + sb);
			return sb.toString();
		});
	}

	@GetMapping("/balance")
	public List<Map<String, Object>> sendBalanceRequest() {
		String data = deepSeekService.getBalanceInfo().blockFirst();
		return ReturnJsonUtils.str2Map(data);
	}

}
